using System;
using System.Collections.Generic;
using System.Text;
using System.Xml.Serialization;
using Weight.Base.Utility;

namespace Weight.Base
{
    /// <summary>
    /// 配置文件实体类
    /// </summary>
    [XmlTypeAttribute(AnonymousType = true)]
    [XmlRootAttribute(Namespace = "", IsNullable = false)]
    public class Setting : BaseSetting
    {
        /// <summary>
        /// 当前配置版本号
        /// </summary>
        //[XmlIgnore]
        //public int CurrentVersion = 3;

        /// <summary>
        /// 界面颜色
        /// </summary>
        public string UIColor = string.Empty;

        /// <summary>
        /// 配置文件的版本号
        /// </summary>

        public DBConn DbConn;
        public Company CompanyName;
        public AutoLogin AutoLoginInfo;
        public TicketBusinessMode TicketMode;
        public DispatchBusinessMode DispatchMode;
        public DispatchInputColumns DispatchInputColumns;
        public DispatchCheckContract DispatchCheckContract;
        /// <summary>
        /// 称重设置
        /// </summary>
        public WeightConfig WeightConfig = new WeightConfig();

        /// <summary>
        /// 称重临时数据显示字段
        /// </summary>
        public List<QueryShowField> WeightShowFields = new List<QueryShowField>();

        /// <summary>
        ///查询合计字段
        /// </summary>
        public List< QuerySumFiled> QuerySumFields = new List<QuerySumFiled>();

        /// <summary>
        /// 查询显示字段
        /// </summary>
        public List<QueryShowField> QueryShowFields = new List<QueryShowField>();

        /// <summary>
        /// 统计合计字段
        /// </summary>
        public List<SummarySumField> SummarySumFields = new List<SummarySumField>();

        /// <summary>
        /// 统计分组字段
        /// </summary>
        public List<SummaryGroupField> SummaryGroupFields = new List<SummaryGroupField>();

        /// <summary>
        /// 查询模板
        /// </summary>
        public QueryExportTemplate QueryTemplate = new QueryExportTemplate();

        /// <summary>
        /// 分析配置
        /// </summary>
        public Analysis Analysis = new Analysis();

        /// <summary>
        /// 发卡配置
        /// </summary>
        public CardManager SendCardConfig = new CardManager();

        /// <summary>
        /// 报表设置
        /// </summary>
        public List<ReportConfig> ReportSetting = new List<ReportConfig>();

        /// <summary>
        /// 打印机设置
        /// </summary>
        public PrinterConfig PrinterSetting;

        /// <summary>
        /// 打印预览设置
        /// </summary>
        public IsPrintViewConfig IsPrintViewConfig;

        /// <summary>
        /// 派车选择框设置
        /// </summary>
        public List<DispatchSelectShowField> DispatchSelectShowFields = new List<DispatchSelectShowField>();

        /// <summary>
        /// 发卡选择框设置
        /// </summary>
        public List<SendCardSelectShowField> SendCardSelectShowFields = new List<SendCardSelectShowField>();

        /// <summary>
        ///默认配置
        /// </summary>
        public Setting()
        {
            WeightConfig = new WeightConfig();
            DbConn = new DBConn();
            CompanyName = new Company();
            AutoLoginInfo = new AutoLogin();
            TicketMode = new TicketBusinessMode()
            {
                memo = "订单模式 0表示按金额,1表示物料数量",
                Mode = 1
            };

            DispatchMode = new DispatchBusinessMode()
            {
                memo = "派车模式 0 表示无订单派车，1表示订单派车",
                Mode = 1
            };

            DispatchInputColumns = new DispatchInputColumns()
            {
                memo = "派车信息录入区的列数",
                Count = 4
            };

            DispatchCheckContract = new DispatchCheckContract()
            {
                memo = "检测派车的预装量是否超出了订单的剩余量 0表示不检测 ，表示检测",
                Mode = 0
            };
            PrinterSetting = new PrinterConfig() { Height = "120", Width = "210", SelectPrinter = string.Empty };

            IsPrintViewConfig = new IsPrintViewConfig() {
                IsPrintView = false,
                Memo = "打印是否预览"
            };
        }

        /// <summary>
        /// 保存默认配置
        /// </summary>
        public override void setDefaultConfig()
        {
            if(QuerySumFields.Count ==0)
            {
                QuerySumFields.Add(new QuerySumFiled() { Field = "Gross" });
                QuerySumFields.Add(new QuerySumFiled() { Field = "Tare" });
                QuerySumFields.Add(new QuerySumFiled() { Field = "Net" });
                QuerySumFields.Add(new QuerySumFiled() { Field = "Impurity" });
                QuerySumFields.Add(new QuerySumFiled() { Field = "Water" });
                QuerySumFields.Add(new QuerySumFiled() { Field = "Amount" });
                QuerySumFields.Add(new QuerySumFiled() { Field = "BalanceAmount" });
                QuerySumFields.Add(new QuerySumFiled() { Field = "Count" });
            }

            if(string.IsNullOrEmpty(Analysis.GroupName) || string.IsNullOrEmpty(Analysis.SumName))
            {
                Analysis.GroupName = "PlateNum";
                Analysis.SumName = "Net";
            }

        }
    }


    #region 称重设置

   /// <summary>
   /// 称重配置
   /// </summary>
    public class WeightConfig
    {
        public WeightConfig()
        {
            IsValueExChange = new AutoExChange() {memo="是否毛皮交换" };
            WeightUnit = new Unit() {memo="计量单位" , Value="kg" };
            ReserveDecimals = new Decimals() {memo="保留小数位",Value=2 };
            WeightInterval = new Interval() {memo="两次过磅的时间间隔(单位：分钟)",Value=2 };
            WeightSteadyTime = new SteadyTime() {memo="重量的稳定时间(单位：秒)",Value=5 };
            WeightStartPoint = new StartPoint() {memo="重量的检测起点" ,Value=100};
            WeightModel = new WeightType() { memo="过磅模式(1:单向过磅、2:双向过磅)" ,Value=1 };
            WeightControls = new WeightControl() {memo="称重控制(0:人工控制、1:无人值守)",Value=0 };
            WeightDataSource = new DataSource() { memo="信息来源(0:手动录入、1:发卡、2:派车单、3:合同订单)"};
            ScaleModel = new WeightScale() { memo = "称重软件的编号", Value = "01", Name = "台号01" };
            WeightCountModel = new WeightCount() { memo = "1：代表单称、2：代表双称", Value = 1 };
            VideoCountModel = new VideoCount() { memo="监控数量设置", Value =6};
            WeightConsumeTicketConfig = new WeightConsumeTicket() { memo="是否扣除订单 true 表示扣除，false 表示不扣除", Value = false };
            OnBangOverTime = new OnBangTimeLimit() { memo= "道闸抬起后,车辆在设置的时间内没有上磅，则道闸自动落下", Value = 10 };
            CloseGateDelay = new CloseGateTimeDelay() { memo="道闸落杆命令在延时时间后落下",Value =5 };
            Simulate = new SimulateConfig() { StartVal=1000, EndVal = 2000, Memo = "模拟重量配置",IsSimulate = false};
        }
        /// <summary>
        /// 是否毛皮交换
        /// </summary>
        public AutoExChange IsValueExChange { get; set; }
        /// <summary>
        /// 计量单位
        /// </summary>
        public Unit WeightUnit { get; set; }

        /// <summary>
        ///保留小数位
        /// </summary>
        public Decimals ReserveDecimals { get; set; }

        /// <summary>
        /// /过磅时间间隔
        /// </summary>
        public Interval WeightInterval { get; set; }

        /// <summary>
        /// 稳定延时
        /// </summary>
        public SteadyTime WeightSteadyTime { get; set; }

        /// <summary>
        /// 检测起点
        /// </summary>
        public StartPoint WeightStartPoint { get; set; }

        /// <summary>
        /// 过磅模式
        /// </summary>
        public WeightType WeightModel { get; set; }

        /// <summary>
        /// 上磅超时限制
        /// </summary>
        public OnBangTimeLimit  OnBangOverTime { get; set; }

        /// <summary>
        /// 道闸落杆延时
        /// </summary>
        public CloseGateTimeDelay CloseGateDelay { get; set; }


        /// <summary>
        /// 称重控制
        /// </summary>
        public WeightControl WeightControls { get; set; }

        /// <summary>
        /// 计量数据来源
        /// </summary>
        public DataSource WeightDataSource { get; set; }

        /// <summary>
        /// 台号
        /// </summary>
        public WeightScale ScaleModel { get; set; }

        /// <summary>
        /// 是否是一机多称
        /// </summary>
        public WeightCount WeightCountModel { get; set; }

        /// <summary>
        /// 监控数量设置
        /// </summary>
        public VideoCount VideoCountModel { get; set; }

        /// <summary>
        /// 称重是否扣除订单配置
        /// </summary>
        public WeightConsumeTicket WeightConsumeTicketConfig { get; set; }

        /// <summary>
        /// 称重设备配置
        /// </summary>
        public List<WeightDeviceControl> WeightDeviceConfig = new List<WeightDeviceControl>();

        /// <summary>
        /// 模拟配置
        /// </summary>
        public SimulateConfig Simulate { get; set; }

        /// <summary>
        /// 是否毛皮交换
        /// </summary>
        public class AutoExChange
        {
            [XmlAttribute]
            public string memo { get; set; }
            /// <summary>
            /// 1:进行毛皮交换、0:不交换
            /// </summary>
            [XmlText]
            public int IsChange { get; set; }
        }

        /// <summary>
        /// 计量单位
        /// </summary>
        public class Unit
        {
            [XmlAttribute]
            public string memo { get; set; }
            [XmlText]
            public string Value { get; set; }
        }

        /// <summary>
        /// 保留小数位
        /// </summary>
        public class Decimals
        {
            [XmlAttribute]
            public string memo { get; set; }
            [XmlText]
            public int Value { get; set; }
        }

        /// <summary>
        ///称重时间间隔 
        /// </summary>
        public class Interval
        {
            [XmlAttribute]
            public string memo { get; set; }
            [XmlText]
            public int Value { get; set; }
        }


        /// <summary>
        /// 稳定延时
        /// </summary>
        public class SteadyTime
        {
            [XmlAttribute]
            public string memo { get; set; }
            [XmlText]
            public int Value { get; set; }
        }


        /// <summary>
        /// 检测起点
        /// </summary>
        public class StartPoint
        {
            [XmlAttribute]
            public string memo { get; set; }
            [XmlText]
            public double Value { get; set; }
        }

        /// <summary>
        /// 上磅超时限制
        /// </summary>
        public class OnBangTimeLimit
        {
            [XmlAttribute]
            public string memo { get; set; }
            [XmlText]
            public int Value { get; set; }
        }

        /// <summary>
        /// 道闸落杆延时
        /// </summary>
        public class CloseGateTimeDelay
        {
            [XmlAttribute]
            public string memo { get; set; }
            [XmlText]
            public int Value { get; set; }
        }

        /// <summary>
        /// 过磅类型
        /// </summary>
        public class WeightType
        {
            [XmlAttribute]
            public string memo { get; set; }
            /// <summary>
            /// 1:单向过磅、2:双向过磅
            /// </summary>
            [XmlText]
            public int Value { get; set; }
        }

        /// <summary>
        /// 称重控制
        /// </summary>
        public class WeightControl
        {
            [XmlAttribute]
            public string memo { get; set; }
            /// <summary>
            /// 0:人工控制、1:无人值守
            /// </summary>
            [XmlText]
            public int Value { get; set; }
        }

        /// <summary>
        /// 称重数据来源
        /// </summary>
        public class DataSource
        {
            [XmlAttribute]
            public string memo { get; set; }
            /// <summary>
            /// 0:手动录入、1:发卡、2:派车单、3:合同订单
            /// </summary>
            [XmlText]
            public int Value { get; set; }
        }

        /// <summary>
        /// 是否是一机双称
        /// </summary>
        public class WeightCount
        {
            [XmlAttribute]
            public string memo { get; set; }
            /// <summary>
            /// 1:单称、2:两个称
            /// </summary>
            [XmlText]
            public int Value { get; set; }
        }

        /// <summary>
        /// 称重台号
        /// </summary>
        public class WeightScale
        {
            [XmlAttribute]
            public string memo { get; set; }

            /// <summary>
            /// 台号名称
            /// </summary>
            public string Name { get; set; }
            /// <summary>
            /// 台号
            /// </summary>
            [XmlText]
            public string Value { get; set; }
        }

        /// <summary>
        /// 称重台号
        /// </summary>
        public class VideoCount
        {
            [XmlAttribute]
            public string memo { get; set; }

            /// <summary>
            /// 监控数量
            /// </summary>
            [XmlText]
            public int Value { get; set; }
        }

        /// <summary>
        /// 过磅是否扣除订单 
        /// </summary>
        public class WeightConsumeTicket
        {
            [XmlAttribute]
            public string memo { get; set; }

            /// <summary>
            /// 是否扣除订单 true 扣除 ，false 不扣除
            /// </summary>
            [XmlText]
            public bool Value { get; set; }
        }

        /// <summary>
        /// 模拟配置
        /// </summary>
        public class SimulateConfig
        {
            /// <summary>
            /// 是否模拟
            /// </summary>
            [XmlText]
            public bool IsSimulate { get; set; }

            /// <summary>
            /// 开始范围
            /// </summary>
            [XmlAttribute]
            public int StartVal { get; set; }

            /// <summary>
            /// 结束范围
            /// </summary>
            [XmlAttribute]
            public int EndVal { get; set; }

            /// <summary>
            /// 备注信息
            /// </summary>
            [XmlAttribute]
            public string Memo { get; set; }
        }

        #region 称重设备配置
        /// <summary>
        /// 称重设备配置
        /// </summary>
        public class WeightDeviceControl
        {
            /// <summary>
            /// 设备标识
            /// </summary>
            [XmlAttribute]
            public int Value { get; set; }

            /// <summary>
            /// 备注
            /// </summary>
            [XmlAttribute]
            public string Memo { get; set; }
        }
        #endregion 称重设备配置
    }

    #endregion 称重设置

    #region 称重查询配置
    /// <summary>
    /// 查询汇总字段配置
    /// </summary>
    public class QuerySumFiled
    {
        [XmlText]
        public string Field { get; set; }
    }

    #endregion 称重查询配置

    #region 订单、派车
    /// <summary>
    /// 订单模式
    /// </summary>
    public class TicketBusinessMode
    {
        [XmlAttribute]
        public string memo { set; get; }
        [XmlText]
        public int Mode { get; set; }
    }

    /// <summary>
    /// 派车模式
    /// </summary>
    public class DispatchBusinessMode
    {
        [XmlAttribute]
        public string memo { set; get; }
        [XmlText]
        public int Mode { get; set; }
    }

    /// <summary>
    /// 派车订单检测
    /// </summary>
    public class DispatchCheckContract
    {
        [XmlAttribute]
        public string memo { set; get; }
        [XmlText]
        public int Mode { get; set; }
    }

    /// <summary>
    /// 列数
    /// </summary>
    public class DispatchInputColumns
    {
        [XmlAttribute]
        public string memo { set; get; }
        [XmlText]
        public int Count { get; set; }
    }

    #endregion 订单派车

    #region 数据库配置
    /// <summary>
    /// 数据库配置
    /// </summary>
    public class DBConn
    {
        public DBConn()
        {
            DbType = new DataBaseType()
            {
                Type = "待配置"
            };
            ServerIp = "待配置";
            Uid = "待配置";
            Pwd = "待配置";
            DabaBase = "待配置";
        }

        /// <summary>
        /// 数据库类型
        /// </summary>
        public DataBaseType DbType { get; set; }


        [XmlAttribute]
        /// <summary>
        /// 数据IP地址
        /// </summary>
        public string ServerIp { get; set; }

        [XmlAttribute]
        /// <summary>
        /// 数据库
        /// </summary>
        public string DabaBase { get; set; }

        [XmlAttribute]
        /// <summary>
        /// 登录账号
        /// </summary>
        public string Uid { get; set; }

        [XmlAttribute]
        /// <summary>
        /// 登录密码
        /// </summary>
        public string Pwd { get; set; }

        /// <summary>
        /// 配置说明
        /// </summary>
        [XmlAttribute]
        public string Memo = "数据库配置";

        /// <summary>
        /// 数据库类型
        /// </summary>
        public class DataBaseType
        {
            /// <summary>
            /// 数据库类型
            /// </summary>
            [XmlText]
            public string Type { get; set; }
            /// <summary>
            /// 备注说明
            /// </summary>
            [XmlAttribute]
            public string Memo = "数据库类型 如下之一:SqlServer、Oracl、MySql";
        }
        public override string ToString()
        {
            string desPwd = new DESHelper().DesDecrypt(Pwd);
            if (DbType.Type == "SqlServer")
            {
                return string.Format("server={0};database={1};uid={2};pwd={3}", ServerIp, DabaBase, Uid, desPwd);
            }
            else if (DbType.Type == "Oracl")
            {
                return string.Format("server={0};database={1};uid={2};pwd={3}", ServerIp, DabaBase, Uid, desPwd);
            }
            else if (DbType.Type == "Oracl")
            {
                return string.Format("server={0};database={1};uid={2};pwd={3}", ServerIp, DabaBase, Uid, desPwd);
            }
            else
            {
                return "";
            }
        }

        public string ToImgString(string year, string company)
        {
            string desPwd = new DESHelper().DesDecrypt(Pwd);
            if (DbType.Type == "SqlServer")
            {
                return string.Format("server={0};database={1};uid={2};pwd={3}", ServerIp, "Image" + year + company, Uid, desPwd);
            }
            else if (DbType.Type == "Oracl")
            {
                return string.Format("server={0};database={1};uid={2};pwd={3}", ServerIp, "Image" + year + company, Uid, desPwd);
            }
            else if (DbType.Type == "Oracl")
            {
                return string.Format("server={0};database={1};uid={2};pwd={3}", ServerIp, "Image" + year + company, Uid, desPwd);
            }
            else
            {
                return "";
            }
        }
    }

    #endregion 数据库配置

    #region 查询、汇总设置
    public class QueryShowField
    {
        /// <summary>
        /// 字段
        /// </summary>
        [XmlAttribute]
        public string Field { get; set; }
        /// <summary>
        /// 显示顺序
        /// </summary>
        [XmlAttribute]
        public int Sort { get; set; }
        /// <summary>
        /// 别名
        /// </summary>
        [XmlText]
        public string Alias { get; set; }
    }

    /// <summary>
    /// 统计分组字段
    /// </summary>
    public class SummaryGroupField
    {
        /// <summary>
        /// 字段
        /// </summary>
        [XmlAttribute]
        public string Field { get; set; }
        /// <summary>
        /// 显示顺序
        /// </summary>
        [XmlAttribute]
        public int Sort { get; set; }
        /// <summary>
        /// 别名
        /// </summary>
        [XmlText]
        public string Alias { get; set; }
    }

    /// <summary>
    /// 统计汇总字段
    /// </summary>
    public class SummarySumField
    {
        /// <summary>
        /// 字段
        /// </summary>
        [XmlAttribute]
        public string Field { get; set; }
        /// <summary>
        /// 显示顺序
        /// </summary>
        [XmlAttribute]
        public int Sort { get; set; }
        /// <summary>
        /// 别名
        /// </summary>
        [XmlText]
        public string Alias { get; set; }
    }

    /// <summary>
    /// 导出模板设置
    /// </summary>
    public class QueryExportTemplate
    {
        /// <summary>
        /// 类型
        /// </summary>
        [XmlAttribute]
        public string Name { get; set; }

        /// <summary>
        /// 字段
        /// </summary>
        [XmlAttribute]
        public string Field { get; set; }

        /// <summary>
        /// 备注
        /// </summary>
        [XmlAttribute]
        public string Memo { get; set; }
    }

    /// <summary>
    /// 分析字段
    /// </summary>
    public class Analysis
    {
        [XmlElement]
        public string GroupName { get; set; }

        [XmlElement]
        public string SumName { get; set; }

    }

    #endregion  查询、汇总设置

    #region 发卡设置
    public class CardManager
    {
        /// <summary>
        /// 发卡数据来源
        /// </summary>
        public CardDataSource CardDataSources = new CardDataSource();

        /// <summary>
        /// 发卡自动获取卡号
        /// </summary>
        public SendCardIsAuto IsAutoSendCard = new SendCardIsAuto();

        /// <summary>
        /// 销卡自动获取卡号
        /// </summary>
        public DestoryIsAuto IsAutoDestory = new DestoryIsAuto();

        public CardManager()
        {
            CardDataSources = new CardDataSource() { memo = "发卡数据来源 0:无来源 ，1：来源发卡数据，2：来源订单" };
        }
    }
    /// <summary>
    /// 发卡的数据来源
    /// </summary>
    public class CardDataSource
    {
        [XmlAttribute]
        public string memo { get; set; }

        /// <summary>
        /// 发卡数据来源 0:无来源 ，1：来源发卡数据，2：来源订单
        /// </summary>
        [XmlText]
        public int Value { get; set; }
    }

    /// <summary>
    /// 是否自动发卡
    /// </summary>
    public class SendCardIsAuto
    {
        /// <summary>
        /// 描述信息
        /// </summary>
        [XmlAttribute]
        public string memo { get; set; }
        /// <summary>
        /// 是否自动读卡
        /// </summary>
        [XmlText]
        public bool IsAuto { get; set; }
    }

    /// <summary>
    /// 是否自动销卡
    /// </summary>
    public class DestoryIsAuto
    {
        /// <summary>
        /// 描述信息
        /// </summary>
        [XmlAttribute]
        public string memo { get; set; }

        /// <summary>
        /// 是否自动销卡
        /// </summary>
        [XmlText]
        public bool IsAuto { get; set; }
    }

    #endregion 发卡设置

    #region 报表设置
    /// <summary>
    /// 打印报表设置
    /// </summary>
    public class ReportConfig
    {
        /// <summary>
        /// 类型名称
        /// </summary>
        [XmlAttribute]
        public string TypeName { get; set; }

        /// <summary>
        /// 文件名称
        /// </summary>
        [XmlText]
        public string FileName { get; set; }
    }

    #endregion 报表设置

    #region 打印机设置
    /// <summary>
    /// 打印机设置实体
    /// </summary>
    public class PrinterConfig
    {
        /// <summary>
        /// 选择的打印机
        /// </summary>
       [XmlAttribute]
        public string SelectPrinter { get; set; }

        /// <summary>
        /// 宽度
        /// </summary>
        [XmlAttribute]
        public string Width { get; set; }

        /// <summary>
        /// 高度
        /// </summary>
        [XmlAttribute]
        public string Height { get; set; }
    }

    public class IsPrintViewConfig
    {
        /// <summary>
        /// 是否预览
        /// </summary>
        [XmlText]
        public bool IsPrintView { get; set; }

        /// <summary>
        /// 备注说明
        /// </summary>
        [XmlAttribute]
        public string Memo { get; set; }
    }
    #endregion 打印机设置

    #region 派车选择框设置
    public class DispatchSelectShowField
    {
        /// <summary>
        /// 字段
        /// </summary>
        [XmlAttribute]
        public string Field { get; set; }
        /// <summary>
        /// 显示顺序
        /// </summary>
        [XmlAttribute]
        public int Sort { get; set; }
        /// <summary>
        /// 别名
        /// </summary>
        [XmlText]
        public string Alias { get; set; }
    }
    #endregion 派车选择框设置

    #region 发卡选择框设置
    public class SendCardSelectShowField
    {
        /// <summary>
        /// 字段
        /// </summary>
        [XmlAttribute]
        public string Field { get; set; }
        /// <summary>
        /// 显示顺序
        /// </summary>
        [XmlAttribute]
        public int Sort { get; set; }
        /// <summary>
        /// 别名
        /// </summary>
        [XmlText]
        public string Alias { get; set; }
    }
    #endregion 发卡选择框设置

}


